package com.ruoyi.web.controller.wms;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.wms.domain.WmsProcureItem;
import com.ruoyi.wms.service.IWmsProcureItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsProcureHead;
import com.ruoyi.wms.service.IWmsProcureHeadService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购申请Controller
 * 
 * @author yy
 * @date 2024-02-15
 */
@Controller
@RequestMapping("/wms/procurehead")
public class WmsProcureHeadController extends BaseController
{
    private String prefix = "wms/procurehead";

    @Autowired
    private IWmsProcureHeadService wmsProcureHeadService;
    @Autowired
    private IWmsProcureItemService wmsProcureItemService;


    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("wms:procurehead:view")
    @GetMapping()
    public String procurehead( )
    {

        return prefix + "/procurehead";
    }

    /**
     * 查询采购申请列表
     */
    @RequiresPermissions("wms:procurehead:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsProcureHead wmsProcureHead)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()){
            wmsProcureHead.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }
        List<WmsProcureHead> list = wmsProcureHeadService.selectWmsProcureHeadList(wmsProcureHead);
        return getDataTable(list);
    }

    /**
     * 导出采购申请列表
     */
    @RequiresPermissions("wms:procurehead:export")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsProcureHead wmsProcureHead)
    {
        List<WmsProcureHead> list = wmsProcureHeadService.selectWmsProcureHeadList(wmsProcureHead);
        ExcelUtil<WmsProcureHead> util = new ExcelUtil<WmsProcureHead>(WmsProcureHead.class);
        return util.exportExcel(list, "采购申请数据");
    }

    /**
     * 新增采购申请
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userName", ShiroUtils.getSysUser().getUserName());
        mmap.put("createTime", DateUtils.parseDateToStr("yyyy-MM-dd",new Date()));
        mmap.put("code", getCode("CG"));
        return prefix + "/add";
    }
    private String getCode(String code){
        int num =  Integer.parseInt(configService.selectConfigByKey("cg.code")) ;
        SysConfig config = new SysConfig();
        config.setConfigKey("cg.code");
        config.setConfigValue((++num)+"");
        configService.updateConfigValue(config);
        return StringUtils.getCode(code ,num);
    }
    /**
     * 新增保存采购申请
     */
    @RequiresPermissions("wms:procurehead:add")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsProcureHead wmsProcureHead)
    {
        return toAjax(wmsProcureHeadService.insertWmsProcureHead(wmsProcureHead));
    }

    /**
     * 修改采购申请
     */
    @RequiresPermissions("wms:procurehead:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsProcureHead wmsProcureHead = wmsProcureHeadService.selectWmsProcureHeadById(id);
        WmsProcureItem item = new WmsProcureItem();
        item.setHeadId(wmsProcureHead.getId());
        wmsProcureHead.setItemList(wmsProcureItemService.selectWmsProcureItemList(item));
        mmap.put("wmsProcureHead", wmsProcureHead);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购申请
     */
    @RequiresPermissions("wms:procurehead:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsProcureHead wmsProcureHead)
    {
        return toAjax(wmsProcureHeadService.updateWmsProcureHead(wmsProcureHead));
    }

    /**
     * 删除采购申请
     */
    @RequiresPermissions("wms:procurehead:remove")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsProcureHeadService.deleteWmsProcureHeadByIds(ids));
    }
}
